package org.apache.camel.component.cmis;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.impl.DefaultEndpoint;
import org.apache.chemistry.opencmis.client.api.Repository;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A CMIS endpoint
 */
public class CmisEndpoint extends DefaultEndpoint {
	
	private static final transient Logger	LOG	= LoggerFactory.getLogger(CmisEndpoint.class);
	private Repository						repository;
	private String							repositoryUri;
	private String							path;
	
	protected CmisEndpoint(String endpointUri, CmisComponent component, String repositoryUri) {
		super(endpointUri, component);
		this.repositoryUri = repositoryUri;
		try {
			String username = CmisConstants.DEFAULT_CMIS_REPOSITORY_USERNAME;
			String password = CmisConstants.DEFAULT_CMIS_REPOSITORY_PASSWORD;
			URI uri = new URI(repositoryUri);
			LOG.debug("UserInfo : {}", uri.getUserInfo());
			LOG.debug("Fragment : {}", uri.getFragment());
			if (uri.getUserInfo() != null) {
				String[] creds = uri.getUserInfo().split(":");
				if (creds != null) {
					username = creds[0];
					LOG.debug("Username : {}", username);
					password = creds.length > 1 ? creds[1] : null;
					LOG.debug("Password : {}", password);
				}
			}
			// default factory implementation
			SessionFactory factory = SessionFactoryImpl.newInstance();
			Map<String, String> parameter = new HashMap<String, String>();
			// user credentials
			parameter.put(SessionParameter.USER, username);
			parameter.put(SessionParameter.PASSWORD, password);
			// connection settings
			parameter.put(SessionParameter.ATOMPUB_URL, uri.toURL().toString());
			parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
			parameter.put(SessionParameter.REPOSITORY_ID, "myRepository");
			repository = factory.getRepositories(parameter).get(0);
			if (repository == null) {
				throw new RuntimeCamelException("No CMIS repository defined under '" + repositoryUri + "'");
			}
			this.path = uri.getFragment();
		} catch (URISyntaxException e) {
			throw new IllegalArgumentException("Invalid URI: " + repositoryUri, e);
		} catch (MalformedURLException e) {
			throw new IllegalArgumentException("Malformed URL: " + repositoryUri, e);
		}
	}
	
	/**
	 * Currently unsupported
	 * 
	 * @throws RuntimeCamelException
	 */
	public Consumer createConsumer(Processor processor) throws Exception {
		throw new RuntimeCamelException("No consumer endpoint support for CMIS available");
	}
	
	public Producer createProducer() {
		return new CmisProducer(this);
	}
	
	public boolean isSingleton() {
		return false;
	}
	
	/**
	 * Get the {@link Repository}
	 * 
	 * @return the repository
	 */
	protected Repository getRepository() {
		return repository;
	}
	
	/**
	 * Get the cmis repository uri
	 * 
	 * @return the repository uri
	 */
	public String getRepositoryUri() {
		return repositoryUri;
	}
	
	/**
	 * Get the cmis path
	 * 
	 * @return the cmis path
	 */
	public String getPath() {
		return path;
	}
}
